package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	v1 "server_go/api/v1"
	"server_go/internal/service"
)

type UsersHandler struct {
	*Handler
	usersService service.UsersService
}

func NewUsersHandler(
	handler *Handler,
	usersService service.UsersService,
) *UsersHandler {
	return &UsersHandler{
		Handler:      handler,
		usersService: usersService,
	}
}

func (h *UsersHandler) Login(ctx *gin.Context) {
	req := new(v1.UsersLoginRequest)
	if err := ctx.ShouldBindJSON(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, err, nil)
		return
	}
	info, err := h.usersService.Login(ctx, *req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, info)
}
